<template>
<!-- 首检任务清单的模态框 -->
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="12">
            <p-form-item label="送检单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                readonly
                v-decorator="[ 'inspectBillno', validatorRules.inspectBillno]"
                placeholder="请输入送检单号"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="车间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                readonly
                v-decorator="[ 'workroomname', validatorRules.workroomname]"
                placeholder="请输入车间"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="二级工序" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'processTwo', validatorRules.processTwo]"
                placeholder="请输入二级工序"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="线体" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-popup
                contextPath="basic-business"
                v-decorator="['linename', validatorRules.linename]"
                :trigger-change="true"
                org-fields="lineid,lineno,linename,workroomid,workroomno,workroomname,linexplain,lineleadername"
                dest-fields="lineid,lineno,linename,workroomid,workroomno,workroomname,linexplain,"
                code="line_select"
                :add="this.url.add"
                isMultiSelect="popup"
                @oNvisible="oNvisible"
                @callback="popupCallback"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="生产工单" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'productionBillno', validatorRules.productionBillno]"
                placeholder="请输入生产工单"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="工序工单" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'processBillno', validatorRules.processBillno]"
                placeholder="请输入工序工单"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="产品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-popup
                contextPath="basic-business"
                v-decorator="['mcode', validatorRules.mcode]"
                :trigger-change="true"
                org-fields="mid,mcode,mname,sprc"
                dest-fields="mid,mcode,mname,sprc"
                code="mes_ba_cpinfo_main"
                :add="this.url.add"
                isMultiSelect="popup"
                @oNvisible="oNvisible"
                @callback="popupCallback"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                readonly
                v-decorator="[ 'mname', validatorRules.mname]"
                placeholder="请输入产品名称"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="操作时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请选择操作时间"
                v-decorator="[ 'operationDt', validatorRules.operationDt]"
                :trigger-change="true"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="检验剩余时长" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'remainingTime', validatorRules.remainingTime]"
                placeholder="请输入检验剩余时长"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="检验状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['inspectStatus', validatorRules.inspectStatus]"
                :trigger-change="true"
                dictCode="mes_inspect_status"
                placeholder="请选择检验状态"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="检验结果" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['inspectResult', validatorRules.inspectResult]"
                :trigger-change="true"
                dictCode="mes_is_qualified"
                placeholder="请选择检验结果"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="检验结论" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'inspectConclusion', validatorRules.inspectConclusion]"
                placeholder="请输入检验结论"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="确认时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请选择确认时间"
                v-decorator="[ 'confirmTime', validatorRules.confirmTime]"
                :trigger-change="true"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="确认人员" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep
                v-decorator="['confirmBy', validatorRules.confirmBy]"
                :trigger-change="true"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="是否抽检" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="radio"
                v-decorator="['isSimple', validatorRules.isSimple]"
                :trigger-change="true"
                dictCode="ba_yn"
                placeholder="请选择是否抽检"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="送检时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请选择送检时间"
                v-decorator="[ 'sendInspectTime', validatorRules.sendInspectTime]"
                :trigger-change="true"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="SN" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'sn', validatorRules.sn]" placeholder="请输入SN"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="检验不合格工站" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number
                v-decorator="[ 'inspectFailWs', validatorRules.inspectFailWs]"
                placeholder="请输入检验不合格工站"
                style="width: 100%"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="当前处理人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep
                v-decorator="['currentHandler', validatorRules.currentHandler]"
                :trigger-change="true"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="复核处理人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep
                v-decorator="['reworkHandler', validatorRules.reworkHandler]"
                :trigger-change="true"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="线号/组件编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'palletno', validatorRules.palletno]"
                placeholder="请输入线号/组件编号"
              ></p-input>
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import { httpAction, putAction, api } from "@/api/manage";
import pick from "lodash.pick";
import JDate from "@/components/jeecg/JDate";
import JSelectUserByDep from "@/components/jeecgbiz/JSelectUserByDep";
import JDictSelectTag from "@/components/dict/JDictSelectTag";

export default {
  name: "MesQcInspectTaskModal",
  mixins: [SubmitOnchangMixin],
  components: {
    JDate,
    JSelectUserByDep,
    JDictSelectTag,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {
        inspectBillno: {},
        workroomname: { rules: [{ required: false, message: "请输入车间!" }] },
        processTwo: { rules: [{ required: false, message: "请输入二级工序!" }] },
        linename: { rules: [{ required: false, message: "请输入线体!" }] },
        productionBillno: {},
        processBillno: {
          rules: [{ required: false, message: "请输入工序工单!" }],
        },
        mcode: { rules: [{ required: false, message: "请输入产品编码!" }] },
        mname: { rules: [{ required: false, message: "请输入产品名称!" }] },
        operationDt: {
          rules: [{ required: false, message: "请输入操作时间!" }],
        },
        remainingTime: {
          rules: [{ required: false, message: "请输入检验剩余时长!" }],
        },
        inspectStatus: {},
        inspectResult: {},
        inspectConclusion: {
          rules: [{ required: false, message: "请输入检验结论!" }],
        },
        confirmTime: {
          rules: [{ required: false, message: "请输入确认时间!" }],
        },
        confirmBy: { rules: [{ required: false, message: "请输入确认人员!" }] },
        isSimple: { rules: [{ required: false, message: "请输入是否抽检!" }] },
        sendInspectTime: {},
        sn: { rules: [{ required: false, message: "请输入SN!" }] },
        inspectFailWs: {
          rules: [{ required: false, message: "请输入检验不合格工站!" }],
        },
        currentHandler: {
          rules: [{ required: false, message: "请输入当前处理人!" }],
        },
        reworkHandler: {
          rules: [{ required: false, message: "请输入复核处理人!" }],
        },
        palletno: {},
      },
      url: {
        add: "/mes-quality/mesQcInspectTask/add",
        edit: "/mes-quality/mesQcInspectTask/edit",
      },
    };
  },
  created() {},
  methods: {
    add() {
      this.edit({});
      this.getRuleCodeValue();
    },
    getRuleCodeValue() {
      let param = {
        commonFormData: this.model,
        rules: [
          { ruleCode: "mes_first_inspect_rule", columnName: "inspectBillno" },
        ],
      };

      putAction(api.ruleBaseURL, param).then((res) => {
        // 执行成功，获取返回的值，并赋到页面上
        if (res.success) {
          console.log("生成的编码：" + res.data);
          const item = res.data;
          this.form.setFieldsValue(pick(item, Object.keys(item)));
        }
      });
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            "inspectBillno",
            "workroomname",
            "processTwo",
            "linename",
            "productionBillno",
            "processBillno",
            "deviceCode",
            "mcode",
            "mname",
            "operationDt",
            "remainingTime",
            "inspectTimes",
            "inspectStatus",
            "inspectResult",
            "inspectConclusion",
            "confirmTime",
            "confirmBy",
            "isSimple",
            "sendInspectTime",
            "sn",
            "inspectFailWs",
            "currentHandler",
            "reworkHandler",
            "palletno"
          )
        );
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          let formData = Object.assign(this.model, values);
          this.formData = Object.assign(this.model, values);
          //新增触发
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
            if (this.EnhanceJS.afterAdd) {
              //新增之前
              let Obj = eval(this.EnhanceJS.afterAdd);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          } else {
            //编辑触发
            httpurl += this.url.edit;
            method = "put";
            if (this.EnhanceJS.afterEdit) {
              //编辑之前
              let Obj = eval(this.EnhanceJS.afterEdit);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          }
        }
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          "inspectBillno",
          "workroomname",
          "processTwo",
          "linename",
          "productionBillno",
          "processBillno",
          "deviceCode",
          "mcode",
          "mname",
          "operationDt",
          "remainingTime",
          "inspectTimes",
          "inspectStatus",
          "inspectResult",
          "inspectConclusion",
          "confirmTime",
          "confirmBy",
          "isSimple",
          "sendInspectTime",
          "sn",
          "inspectFailWs",
          "currentHandler",
          "reworkHandler",
          "palletno"
        )
      );
    },
  },
};
</script>
